package com.agua.enturno.vista.paginas.prestador;

import com.agua.enturno.servicios.ServicioService;
import com.agua.enturno.vista.dto.AgendaDTO;
import com.agua.enturno.vista.dto.ServicioDTO;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.ListView;
import org.apache.wicket.markup.html.panel.Panel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.PropertyModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import java.util.List;

public class ServiciosPanel extends Panel {

	private static final long serialVersionUID = 1L;
	
	@SuppressWarnings("unused")
	private List<AgendaDTO> agendas;
	
	private AgendasDeServicioPanel agendasDeServicioPanel;
	
	@SpringBean
    private ServicioService servicioService;
	

	public ServiciosPanel(String id, IModel<List<ServicioDTO>> model) {
		super(id, model);
		
		agendas = servicioService.findAgendas(model.getObject().get(0).getId());
		
		add(new ListView<ServicioDTO>("servicio", model) {
			private static final long serialVersionUID = 1L;

			@Override
			protected void populateItem(ListItem<ServicioDTO> item) {
				
				final ServicioDTO servicio = item.getModelObject();
				
				AjaxLink<String> servicioLink = new AjaxLink<String>("servicio-link") {
					private static final long serialVersionUID = 1L;

					@Override
					public void onClick(AjaxRequestTarget target) {
						
						agendas = servicioService.findAgendas(servicio.getId());
						target.add(agendasDeServicioPanel);
						target.appendJavaScript("selectService();");
					}
				};
				servicioLink.add(new Label("servicio-label", servicio.getNombre()));
				item.add(servicioLink);
			}
		});
		
		agendasDeServicioPanel = new AgendasDeServicioPanel("agendas-de-servicio", new PropertyModel<List<AgendaDTO>>(this, "agendas"));
		add(agendasDeServicioPanel);
	}

}
